#ifndef AVMUPDATEITEMPANEL_H
#define AVMUPDATEITEMPANEL_H

#include <wx/wx.h>
#include <map>
#include "flash_tool.h"

typedef std::map<const char*, const char*> MountPointsMap;

class AvmUpdateItemPanel : public wxPanel
{
private:
    static MountPointsMap TmpMountPoints;
public:
    static const wxEventType Evt_DeleteItem;
public:
    AvmUpdateItemPanel(wxWindow* parent, bool new_add = true, const wxString& defaultdir = wxEmptyString);
    virtual ~AvmUpdateItemPanel();

    void SetNewFlag(bool new_add);
    bool GetNewFlag() const { return is_new;}

    int GetUpdateItemType() const;

    wxString GetSourceFile() const;
    wxString GetTargetPath() const;
    void SetTargetPath(const wxString& path);
    off_t GetEmmcPosition() const;
    void SetEmmcPosition(uint64_t off);
    wxString GetCommandLine() const;
    void SetCommandLine(const wxString& cmd);

    bool AppendUpdateItem(const wxString& output = wxEmptyString);
    bool UpdateInformation(const SectionHeader& item);
    void SetUpdateItemType(int type);

    bool DumpMakeCommand(const wxString& cmd_path);

    void SetAutoremoveFlag(bool autoremove = true) { auto_remove = autoremove; }

    void SetSourceBinaryPath(const wxString& source)
    {
        source_binary_path = source;
    }
    void SetSourceBinaryOffset(wxFileOffset offset)
    {
        source_offset = offset;
    }
    void SetSourceBinarySize(off_t size)
    {
        source_size = size;
    }

private:
    wxSizerItem* src_file_sizer;
    wxSizerItem* dst_file_sizer;
    wxSizerItem* emmc_pos_sizer;
    wxSizerItem* cmd_sizer;
    wxSizerItem* process_sizer;
    wxSizerItem* extract_sizer;
    bool ui_ok;
    bool is_new;
    bool auto_remove;
    wxComboBox* cmb_type;
    wxTextCtrl* txt_src;
    wxButton* btn_browse_src;
    wxComboBox* cmb_dst_part;
    wxTextCtrl* txt_dst;
    wxComboBox* cmb_emmc_pos;
    wxTextCtrl* txt_cmdline;
    wxButton* btn_extract;
    wxString output_bin;

    wxString source_binary_path;//如果是文件加载的，那么此路径记录了来源文件
    wxFileOffset source_offset;//来源文件偏移
    off_t    source_size;

private:
    void InitUI();
    void SelectUpdateItemType(int sel);
    bool AppendCommandItem(const wxString& output);
    bool AppendFilesystemItem(const wxString& output);
    bool AppendRawItem(const wxString& output);
    void PreprocessCmdLine(wxString& cmdline, bool reverse_convert = false);
    bool DumpCommand(const wxString& cmd_path);
    bool DumpFsfile(const wxString& cmd_path);
    const char* GetItemMountPoint();
    bool DumpRawdata(const wxString& cmd_path);
    bool AppendMakeCommand(const wxString& cmd_path, const wxString& cmd);//在文件cmd_path结尾追加命令cmd
    static bool SaveDataFromFile(const wxString& src, wxFileOffset offset, off_t length, const wxString& dst);
protected:

private:
    void OnUpdateItemTypeChange(wxCommandEvent& event);
    void OnBrowseSourceFile(wxCommandEvent& event);
    void OnBtnExtract(wxCommandEvent& event);
    void OnBtnDelete(wxCommandEvent& event);
    DECLARE_EVENT_TABLE();
};

#endif // AVMUPDATEITEMPANEL_H
